package com.quzhi.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quzhi.common.Utils;
import com.quzhi.sys.entity.*;
import com.quzhi.sys.mapper.TransferRecordMapper;
import com.quzhi.sys.service.ITransferRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Xibing
 * @since 2024-07-23
 */
@Service
public class TransferRecordServiceImpl extends ServiceImpl<TransferRecordMapper, TransferRecord> implements ITransferRecordService {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private InventoryServiceImpl inventoryService;

    @Autowired
    private ProductServiceImpl productService;

    @Autowired
    private TransferGoodsServiceImpl transferGoodsService;


    /**
     *  订单批量调拨
     * @param outWarehouseId 出库名称
     * @param saveWarehouseId  入库名称
     * @param serialNumbers 序列号列表
     * @param token token
     */
    @Override
    public void TransferGoods(String outWarehouseId, String saveWarehouseId, List<String> serialNumbers, String token) {
        // 生成调拨单号
        TransferRecord transferRecord = new TransferRecord();

        // 生成调拨单号
        String db = Utils.generateRandomNumberString("DB");
        User userinfoForBack = userService.getUserinfoForBack(token);

        // 调拨单
        transferRecord.setTransferId(db);
        transferRecord.setOutWarehouseId(outWarehouseId);
        transferRecord.setSaveWarehouseId(saveWarehouseId);
        transferRecord.setRemark("订单批量调拨序列号");
        transferRecord.setCreateTime(LocalDateTime.now());
        transferRecord.setOperator(userinfoForBack.getUsername());
        // 保存
        this.baseMapper.insert(transferRecord);

        // 查询所有在库的商品
        LambdaQueryWrapper<Inventory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Inventory::getWarehouseId, transferRecord.getOutWarehouseId());
        List<Inventory> inventories = inventoryService.list(lambdaQueryWrapper);
        // 添加需要调拨的商品

        for (String serialNumber : serialNumbers) {
            for (Inventory inventory : inventories) {
                if(serialNumber.equals(inventory.getSerialNumber())){
                    // 库存调拨

                    // 调拨记录
                    TransferGoods transferGoods = new TransferGoods();
                    transferGoods.setId(UUID.randomUUID().toString());

                    transferGoods.setTransferRecordId(transferRecord.getTransferId());

                    // 调拨
                    inventory.setWarehouseId(transferRecord.getSaveWarehouseId());
                    inventoryService.updateById(inventory);

                    transferGoods.setInventoryId(inventory.getId());
                    // 查询商品名称
                    Product byId = productService.getById(inventory.getProductId());
                    transferGoods.setGoodsName(byId.getGoodsName()); // 保存商品名称

                    //   保存调拨记录
                    transferGoodsService.save(transferGoods);
                    break;
                }
            }
        }

    }
}
